package com.xyy.saas.payment.controller;

import com.alibaba.fastjson.JSON;
import com.xyy.saas.payment.service.auth.AuthService;
import com.xyy.saas.payment.pojo.bo.QueryAccountBo;
import com.xyy.saas.payment.pojo.bo.QueryPermission;
import com.xyy.saas.payment.pojo.vo.ResultIntVo;
import com.xyy.saas.payment.manage.cores.vo.ResultPageVo;
import com.xyy.saas.payment.manage.cores.vo.ResultVO;
import com.xyy.saas.payment.pojo.vo.PayUserVo;
import com.xyy.saas.payment.service.auth.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户信息接口
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class PayUserController {

    @Resource(name = "manageUserService")
    private UserService userService;

    @Autowired
    private AuthService authService;

    /** 编辑用户信息接口 */
    @PostMapping(value = "/edit",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String editUser(@RequestBody String json){
        log.info("编辑用户信息参数:{}",json);
        ResultVO resultVo = userService.editUser(json);
        return JSON.toJSONString(convertToInt(resultVo));
    }

    /** 新增用户信息接口 */
    @PostMapping(value = "/add",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addUser(@RequestBody String json){
        log.info("新增用户信息参数:{}",json);
        ResultVO resultVo = userService.addUser(json);
        return JSON.toJSONString(convertToInt(resultVo));
    }

    /** 获取单个账号的权限 */
    @RequestMapping(value = "/getPermission",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getPermission(@RequestBody QueryPermission queryPermission){
        ResultVO resultVo = new ResultVO();
        QueryAccountBo queryAccountBo = new QueryAccountBo();
        try {
            queryAccountBo.setAccount(queryPermission.getAccount());
            authService.getDataAndMenu(queryAccountBo,"singal");
            resultVo.setCode("0");
            resultVo.setMsg("成功");
            resultVo.setResult(queryAccountBo);
        }catch (Exception e){
            log.error("{}获取权限异常:{}",queryPermission.getAccount(),e);
            resultVo.setCode("1");
            resultVo.setMsg("获取权限异常,请联系管理员");
        }
        return JSON.toJSONString(convertToInt(resultVo));
    }

    /** 分页获取用户信息 */
    @PostMapping(value = "/getUser",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getUsers(@RequestBody String json){
        log.info("查询用户信息参数:{}",json);
        ResultPageVo<PayUserVo> pageVo = userService.getUsers(json);
        return JSON.toJSONString(pageVo);
    }

    private ResultIntVo convertToInt(ResultVO resultVo){
        ResultIntVo intVo = new ResultIntVo();
        intVo.setCode(Integer.valueOf(resultVo.getCode()));
        intVo.setMsg(resultVo.getMsg());
        intVo.setResult(resultVo.getResult());
        return intVo;
    }
}
